package com.lfk.justweengine.utils.music;

import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.HashMap;

public class SoundManagerOhosTest {

    @Test
    public void testAddSound() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertFalse(musicMap.isEmpty());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testRemoveSound() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        soundManager.removeSound("Test");
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertTrue(musicMap.isEmpty());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testPlay() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        soundManager.play("Test",1.1f);
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertFalse(musicMap.isEmpty());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testTestPlay() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        soundManager.play("Test");
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertFalse(musicMap.isEmpty());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testTestPlay1() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        soundManager.play(0);
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertEquals(0,musicMap.get("Test").intValue());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testTestPlay2() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        soundManager.play(0,1.1f);
        try {
            Field f = soundManager.getClass().getDeclaredField("musicMap");
            f.setAccessible(true);
            HashMap<String, Integer> musicMap = (HashMap<String, Integer>) f.get(soundManager);
            Assert.assertEquals(0,musicMap.get("Test").intValue());
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testContainSound() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        Assert.assertTrue(soundManager.containSound("Test"));
    }

    @Test
    public void testContainSoundID() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        Assert.assertTrue(soundManager.containSoundID(0));
    }

    @Test
    public void testGetSoundID() {
        MainAbilityTest m = new MainAbilityTest();
        SoundManager soundManager = new SoundManager(m,2);
        soundManager.addSound("Test");
        Assert.assertEquals(0,soundManager.getSoundID("Test"));
    }

}